export function checkIsPhone(str) {
  return /^1[3|4|5|7|8|9][0-9]\d{8}$/.test(str);
}

export function handleCall(phoneNumber) {
  // 判断是否为非空字符串
  if (typeof phoneNumber !== "string" || phoneNumber.trim() === "") {
    console.error("电话号码不能为空");
    return;
  }
  // 调用uni.makePhoneCall API
  uni.makePhoneCall({
    phoneNumber: phoneNumber, // 电话号码
    success: function () {
      console.log("拨号成功");
    },
    fail: function (err) {
      console.error("拨号失败：", err);
    },
  });
}

export function transTime(time) {
  if (time <= 0) {
    return [0, 0, 0];
  }
  let second = parseInt(time % 60);
  let minute = parseInt((time / 60) % 60);
  let hour = parseInt(time / 60 / 60);
  return [hour, minute, second];
}

export function transTimeNum(time) {
  const [hour, minute, second] = transTime(time);
  let sec = second < 10 ? "0" + second : second;
  let min = minute < 10 ? "0" + minute : minute;
  let hou = hour < 10 ? "0" + hour : hour;
  return min + ":" + sec;
}

export function transTimeStr(time) {
  const [hour, minute, second] = transTime(time);
  let res = "";
  if (hour) {
    res += `${hour}时`;
  }
  if (minute) {
    res += `${minute}分`;
  }
  if (second) {
    res += `${second}秒`;
  }
  return res;
}

export function uuid() {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (
    S4() +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    S4() +
    S4()
  );
}

export function dateFormat(time, format) {
  let ret;
  const date = new Date(time);
  const options = {
    "Y+": date.getFullYear().toString(), // 年
    "M+": (date.getMonth() + 1).toString(), // 月
    "D+": date.getDate().toString(), // 日
    "H+": date.getHours().toString(), // 时
    "m+": date.getMinutes().toString(), // 分
    "s+": date.getSeconds().toString(), // 秒
    "z+": date.getMilliseconds().toString(), // 毫秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (const k in options) {
    ret = new RegExp("(" + k + ")").exec(format);
    if (ret) {
      format = format.replace(
        ret[1],
        ret[1].length == 1
          ? options[k]
          : options[k].padStart(ret[1].length, "0")
      );
    }
  }
  return format;
}
